package my.suveng.oauth2.vender.NetEase.email;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.HexUtil;
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import cn.hutool.crypto.symmetric.SymmetricCrypto;
import cn.hutool.extra.mail.MailAccount;
import cn.hutool.extra.mail.MailUtil;
import my.suveng.model.common.interfaces.response.IMessage;
import my.suveng.model.common.response.Message;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;

import java.util.concurrent.Future;

@Service
public class NetEaseEmailServiceImpl implements IEmailService {

    private static MailAccount mailAccount = new MailAccount();

    static {
        SymmetricCrypto aes = new SymmetricCrypto(SymmetricAlgorithm.AES,
                HexUtil.decodeHex("33fd069f3974134b562bb9992ca954d9"));
        mailAccount.setHost("smtp.163.com");
        mailAccount.setPort(587);
        mailAccount.setAuth(true);
        mailAccount.setConnectionTimeout(20000);
        mailAccount.setSslEnable(true);
        mailAccount.setStarttlsEnable(true);
        mailAccount.setTimeout(50000);
        mailAccount.setFrom("suveng@163.com");
        mailAccount.setUser("suveng");
        mailAccount.setPass(aes.decryptStr("5e9e6d0e56c0fe433a8e532bd430d335", CharsetUtil.CHARSET_UTF_8));
    }

    @Override
    public IMessage<Object> send(String address, String subject, String content) {
        MailUtil.send(mailAccount, CollUtil.newArrayList(address), subject, content, false);
        return Message.success();
    }

    @Async
    @Override
    public Future<IMessage<Object>> sendAsync(String address, String subject, String content) {
        return AsyncResult.forValue(send(address, subject, content));
    }

}
